﻿<%@ page language="java" import="java.util.*,java.sql.*,com.ruohi.util.dbutil.*,com.ruohi.util.common.*,com.ruohi.manage.*" pageEncoding="UTF-8"%>
<%@page import="com.ruohi.util.dbutil.DBHelper"%>
<%
	String tablePrefix = Env.getInstance().getProperty("table_prefix");
	String tableName = "gift_product";
	String channelUUID = CommonString.getFormatPara(request.getParameter("channel_uuid"));
	String store_id = CommonString.getFormatPara(request.getParameter("store_id"));
	String fileds = "id,uuid,no,name,category,brand,storage,weight,view_count,is_show,order_no,is_rec_index,(select count(*) from gift_product_comment where gift_product_comment.product_no=gift_product.no) as comment_count";
	String whereStr = " ";
	String orderByStr = " id desc ";
	List<Map> brandList = (ArrayList)application.getAttribute("giftBrandList");
	//产品类别
	Map categoryMap = new TreeMap();
	
	List<Map> categoryList = (List)application.getAttribute("giftCategoryList");
	String n = "";
	String name = "";
	if(categoryList != null){
		for(Map m : categoryList){
			n = (String)m.get("no");
			name = (String)m.get("name");
			name = CommonForm.getHtmlSpaceByString((String)m.get("parent_no"))+name;
			categoryMap.put(n,name);
		}
	}
	
	DBHelper dbHelper = DBHelper.getInstance() ;
	Connection connection = dbHelper.getConnection("business");
	String productids = CommonString.getFormatPara(request.getParameter("product_id")) ;
	try{
	productids = productids.replace("'","") ;
	String[] products_id = productids.split(",") ;
	int result = 0 ;
	if(products_id.length>0){
		String insert_sql = "" ;
		for(int i=0;i<products_id.length;i++){
			String pro_sql="select * from gift_product_store where store_id=? and product_id=?";
				List<Map> productList=dbHelper.getMapList(pro_sql,new Object []{store_id,products_id[i]},connection);
			if(productList.size()>0 && productList!=null ){
				continue;
			}else{
			insert_sql = "insert into gift_product_store(store_id,product_id,uuid) values('"+store_id+"','"+products_id[i]+"','"+UUID.randomUUID().toString()+"'"+")";
			if(!"".equals(products_id[i])){
				result += dbHelper.execute(insert_sql,connection) ;
			}
			}
		}		
	}
	if(result>0){
		products_id = null ;
		result = 0 ;
		%>
		<script>
		  alert("产品追加成功！");	 
		</script>
		<%
	}
}catch(Exception e){
	e.printStackTrace();
}finally{
	dbHelper.freeConnection(connection);
}
%><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <title><%=Env.getInstance().getProperty("project_name") %></title>
	<link href="/plugin/ui/ligerUI/ligerUI/skins/Aqua/css/ligerui-all.css" rel="stylesheet" type="text/css" />
	<link href="/plugin/ui/ligerUI/ligerUI/skins/Gray/css/all.css" rel="stylesheet" type="text/css" />
	<link href="/plugin/ui/ligerUI/ligerUI/skins/ligerui-icons.css" rel="stylesheet" type="text/css" />
	<link href="/backend/style/style.css" rel="stylesheet" type="text/css" />
	<script src="/plugin/jquery/jquery.min.js" type="text/javascript"></script>
  </head>
  <body>
  	<div class="searchForm">
  	<form name="theform" id="theform" action="" method="post">
  		<div class="searchCondition">
        	<div class="searchLeft">编号</div>
	  		<div class="searchRight">
	  			<input type="text" style="width: 120px;" name="no" class="jr_text" id="no" />
	  		</div>
	  		<div class="searchLeft">名称</div>
	  		<div class="searchRight">
	  			<input type="text" style="width: 120px;" name="name" class="jr_text" id="name" />
	  		</div>
	
	  		<div style="clear: left;"></div>
            <div class="searchLeft">类别</div>
	  		<div class="searchRight">
	  			<select class="jr_select" id="category" name="category" >
                    <option value="">--请选择类别--</option>
                    <%=CommonForm.createSelect(categoryMap, "") %>
                </select>
	  		</div>
            <div class="searchLeft">品牌</div>
	  		<div class="searchRight">
	  			  <select class="jr_select" id="brand" name="brand">
                    <option value="">-请选择品牌-</option>
                    <%if(brandList != null){for(Map m : brandList){%>
                    <option value="<%=m.get("no")%>" ><%=CnToFullSpell.getFirstSpell(CommonString.getFormatPara(m.get("name"))).length()>=1?CnToFullSpell.getFirstSpell(CommonString.getFormatPara(m.get("name"))).substring(0,1):""%>-<%=CommonString.getFormatPara(m.get("name"))%></option>
                    <%}}%>
                  </select>
	  		</div>	
	  		
  		</div>
  		
  		<input type="hidden" name="product_id" id="product_id"/>
	  </form>
  		<div class="searchBtn">
  			<input type="button" class="btn" onClick="doSearch();" value="查询" />
  			<input type="button" class="btn" onClick="doClearForm();" value="重置" />
  		</div>
  	</div>
	<div style="clear: left;"></div>
	<div class="l-loading" style="display: block" id="pageloading"></div> 
	<div id="maingrid" style="margin: 0; padding: 0"></div>
	<div style="display: none;"></div>
  </body>
</html>
<script src="/plugin/ui/ligerUI/ligerUI/js/ligerui.min.js" type="text/javascript"></script>
<script src="/backend/scripts/common.js" prefix="<%=tablePrefix %>" type="text/javascript"></script>
<script type="text/javascript">
	var grid;
	var tableName = "<%=tableName %>";
	var channelUUID = '<%=channelUUID%>';
	var fields = "<%=fileds %>";
	var whereStr = "<%=whereStr %>";
	var orderBy = "<%=orderByStr %>";
	var columnsArr = [];
	var url = "";
	var conditionStr = "";
	var params = [];
	var myToolbar = {
        items: [{
            text: '确定增加商品',
           click : function(){
     								var checkedRows = grid.getCheckedRows();
									var idStr = "";
										var facidStr = "" ;
										
										for (var i = 0; i < checkedRows.length; i++) {
											idStr += "'" + checkedRows[i].id + "',";
										}
										if (idStr.indexOf(",") != -1 ) {
											idStr = idStr.substring(0, idStr.length - 1);
											jQuery("#product_id").val(idStr) ;
									
											 jrConfirm("确定导入吗?",function(yes){
											 	if(true==yes){
											 		 jQuery("#theform").submit() ;  //提交表单
											 	}
											 }) ;
										}
									},
            icon: 'add'
        },{
            text: '打开查询',
            click: function(){
                closeAndOpen();
            },
            icon: 'right'
        }]
    };
	jQuery(function() {
		params.push({'name':'enumNo','value':'106013','fileName':'test'});
		grid = new Dev21sunGrid({
			toolbar:myToolbar
			});
		jQuery("#pageloading").hide();
	});
	//列数组
	columnsArr =  [ 
	{ display : '名称', name : 'name'  , id : 'no', align : 'left' , type : 'string' , width : 280
		} ,
	{ display : '编号', name : 'no'  , align : 'left' , type : 'string' , width : 150 
	}  ,
	{ display : '类别', name : 'category'  , align : 'left' , type : 'string' , width : 120 , render : function(row){
		return getGiftCategory(row.category,"giftCategoryMap");	
	}} ,
	{ display : '品牌', name : 'brand'  , align : 'left' , type : 'string' , width : 100 , render : function(row){
		return getGiftBrand(row.brand,"giftBrandMap");	
	}} 
	];

	/*列表页条件查询*/
	function doSearch(){
		params = [];
		conditionStr = "";
		var no = jQuery("#no").val();
		if(''!=jQuery.trim(no)){
			conditionStr += " and no like '%"+no+"%' ";
		}
		var name = jQuery("#name").val();
		if(''!=jQuery.trim(name)){
			conditionStr += " and name like '%"+name+"%' ";
		}
		var category = jQuery("#category").val();
		if(''!=jQuery.trim(category)){
			conditionStr += " and category like '"+category+"%' ";
		}
		var brand = jQuery("#brand").val();
		if(''!=jQuery.trim(brand)){
			conditionStr += " and brand = '"+brand+"' ";
		}
		params.push({'name':'tableName','value':tableName});
		params.push({'name':'enumNo','value':'106013'});
		params.push({'name':'fields','value':fields});
		params.push({'name':'whereStr','value':conditionStr});
		params.push({'name':'orderBy','value':orderBy});
		grid.options.parms=params;
		grid.loadData();
	}
	
/**
 * 新品推荐方法
 * 
 * @param {}
 *            arg 推荐时要传的参数
 */
function setRec(state) {
	var art;
	if (typeof(arg) == 'undefined' || arg == null) {
		arg = {};
	}
	var checkedRows = grid.getCheckedRows();
	if (checkedRows.length <= 0) {
		return;
	}
	// 遍历所有的选中行，如果是没有UUID，则移除
	jQuery.each(checkedRows, function(index, data) {
				if (typeof(this.uuid) == 'undefined') {
					jrDeleteRow(this.__index);
				}
			});
	jrConfirm("确定推荐选中项吗？", function(v) {
				if (v) {
					var checkedRows = grid.getCheckedRows();
					var idStr = "";
					for (var i = 0; i < checkedRows.length; i++) {
						idStr += "'" + checkedRows[i].uuid + "',";
					}
					if (idStr.indexOf(",") != -1) {
						idStr = idStr.substring(0, idStr.length - 1);
					}
					arg.del_id = idStr;
					if ('undefined' == typeof(arg.tableName)) {
						arg.tableName = tableName;
					}
					arg.state = state;
					executeRec(arg);
				}
			});
}

/**
 * 执行删除的方法
 * 
 * @param {}
 *            data 要传向后台的数据
 */
function executeRec(data) {
	var enumNo = '';
	for (var i = 0; i < params.length; i++) {
		if (params[i].name == 'enumNo') {
			enumNo = params[i].value;
		}
	}
	jQuery.ajax({
				url : '/backend/action/ajax_cud.jsp?enumNo=' + enumNo + '&flag=setRec',
				data : data,
				type : 'post',
				async : false,
				cache : false,
				success : function(data) {
					grid.loadData();
				}
			});
}

</script>
